using UnityEngine;
using System.Collections;

public class ArrowScript : MonoBehaviour {

    private GameObject[] obs;
    private Vector3 moveDirection;
    public float speed = 5.0f;
    public float damage = 5.0f;
    private float life = 0;
    
    // Use this for initialization
    void Start()
    {

    }
    
    // Update is called once per frame
    void Update()
    {
        if(Network.isServer){
            life += 1;
            if (life > 100)
                GameObject.Destroy(gameObject);
            moveDirection = transform.TransformDirection(Vector3.forward);
            moveDirection.Normalize();
            this.transform.position += Vector3.Scale(moveDirection,new Vector3(speed,speed,speed));
            networkView.RPC("UpdateArrowPosition",RPCMode.Server,transform.position,transform.rotation);
        }
    }
    
    void OnTriggerEnter(Collider other){
        EnemyHealth e = other.transform.GetComponent<EnemyHealth>();
        if(e != null)
            e.damage(damage);
        
        PlayerHealth p = other.transform.GetComponent<PlayerHealth>();
        if(p != null)
            p.damage(damage);
            
        GameObject.Destroy(gameObject);
    }
    
    [RPC]
    void UpdateArrowPosition(Vector3 pos, Quaternion rot){
        transform.position = pos;
        transform.rotation = rot;
    }
}